<template>
	<a-modal :title="title" :width="500" :visible="visible" :confirmLoading="confirmLoading" @ok="handleOk" @cancel="handleCancel" :destroyOnClose="true" :maskClosable="false">
		<a-spin :spinning="confirmLoading">
			<a-form :form="form" autocomplete="off">
				<div style="margin-bottom: 10px;">
					请输入
					<span style="color: #f00;" v-if="type == 0">{{ mdl.realName }}（{{ mdl.username }}）</span>
					<span style="color: #f00;" v-else>选中的{{ userIds.length }}个用户</span>
					的新密码
				</div>
				<a-form-item extra="长度为6~30位，必须包含数字、字母、符号中至少一种">
					<a-input-password
						placeholder="请输入密码"
						v-decorator="[
							'password',
							{
								rules: [{ required: true, message: '请输入密码' }, { pattern: PwdReg, message: '长度为6~30位，必须包含数字、字母、符号中至少一种' }]
							}
						]"
					/>
				</a-form-item>
			</a-form>
		</a-spin>
	</a-modal>
</template>

<script>
import { PwdReg } from '@/utils/validate';
import { ModalMixins } from '@/mixins';
import { sysManageapi } from '@/api';
import md5 from 'md5';
export default {
	name: 'restPasswordModal',
	data() {
		return {
			PwdReg,
			title: '重置密码',
			type: 0, //批量还是单个
			userIds: []
		};
	},
	mixins: [ModalMixins],
	methods: {
		resetPwd(type, record) {
			console.log("重置密码账户数据"+JSON.stringify(record))
			this.type = type;
			if (type == 0) {
				let { username, realName, userId } = record;
				this.mdl = { username, realName, userId };
				this.userIds = [userId];
			} else {
				this.userIds = [...record];
			}
			this.visible = true;
		},
		handleOk() {
			const _this = this;
			// 触发表单验证
			this.form.validateFields((err, values) => {
				// 验证表单没错误
				if (!err) {
					let { password } = values;
					let parameter = { userIds: this.userIds };
					//let parameter = { userIds: this.userIds, password };
					parameter.md5Pass=md5(password)
					_this.confirmLoading = true;
					this.$postAction(sysManageapi.userUrl.resetPwd, parameter)
						.then(res => {
							if (res && res.code == 1) {
								_this.$message.success('重置密码成功');
								this.visible = false;
							}
						})
						.finally(() => {
							_this.confirmLoading = false;
						});
				}
			});
		}
	}
};
</script>

<style scoped></style>
